##############################################################
#Figure B7: Jackknife Estimates
##############################################################

sen.prec <- read.dta13("./temp/figb7adata.dta")

sen.prec$jack <- c(1,2,3,4,5,6,7,1,2,3,4,5,6,7,1,2,3,4,5,6,7)
sen.prec$varnew <- c("ci_lower","ci_lower","ci_lower","ci_lower","ci_lower","ci_lower","ci_lower", 
                     "ci_upper", "ci_upper", "ci_upper", "ci_upper", "ci_upper", "ci_upper", "ci_upper",
                     "coef","coef","coef","coef","coef","coef","coef")
sen.prec <- sen.prec[,-1]

newdf <- sen.prec %>% 
  gather(variable, value, -varnew, -jack)%>%
  spread(varnew, value)

newdf$id <- c(5,4,3,2,6,1)

sen.hur<- ggplot() + 
  geom_pointrange(data=newdf, mapping=aes(x=id, y=coef, ymin=ci_upper, 
                                          ymax=ci_lower, fill=as.factor(jack)), color="grey60", size=1.2, lwd=1.2, fatten=1.5, shape=21) +
  ylim(-.1,.1) +
  scale_x_discrete(limit=c("1", "2", "3", "4", "5", "6"), labels= c("[500,30)", "[400,30)", "[300,30)", "[200,30)", "[100,30)", "[50,30)")) + 
  xlab("Distance Band")  + ylab("Estimate on Democrat Share U.S. Senate") +
  geom_hline(yintercept=0, col="grey60", lwd=1, lty="dashed", alpha=.6)  + 
  theme(plot.title = element_text(hjust = .5, face="bold", size=15, colour = "grey60"),
        axis.text= element_text(size = 12),
        axis.title = element_text(size=15)) + 
  scale_fill_manual(values=c("darkgoldenrod4", "darkgoldenrod", "darkgoldenrod3", "darkgoldenrod2", "darkgoldenrod1", "goldenrod2", "goldenrod1"),
                    labels=c("LA","NC","SC","TX","VA","MS","AL"), name="Jackknifed")
sen.hur

rm(newdf, sen.prec)
#House 
house.prec <- read.dta13("./temp/figb7bdata.dta")

house.prec$jack <- c(1,2,3,4,5,6,7,1,2,3,4,5,6,7,1,2,3,4,5,6,7)
house.prec$varnew <- c("ci_lower","ci_lower","ci_lower","ci_lower","ci_lower","ci_lower","ci_lower", 
                       "ci_upper", "ci_upper", "ci_upper", "ci_upper", "ci_upper", "ci_upper", "ci_upper",
                       "coef","coef","coef","coef","coef","coef","coef")
house.prec <- house.prec[,-1]

newdf <- house.prec %>% 
  gather(variable, value, -varnew, -jack)%>%
  spread(varnew, value)

newdf$id <- c(5,4,3,2,6,1)


house.hur<- ggplot() + 
  geom_pointrange(data=newdf, mapping=aes(x=id, y=coef, ymin=ci_upper, 
                                          ymax=ci_lower, fill=as.factor(jack)), color="grey60", size=1.2, lwd=1.2, fatten=1.5, shape=21) +
  ylim(-.1,.1) +
  scale_x_discrete(limit=c("1", "2", "3", "4", "5", "6"), labels= c("[500,30)", "[400,30)", "[300,30)", "[200,30)", "[100,30)", "[50,30)")) + 
  xlab("Distance Band")  + ylab("Estimate on Democrat Share U.S. House") +
  geom_hline(yintercept=0, col="grey60", lwd=1, lty="dashed", alpha=.6)  + 
  theme(plot.title = element_text(hjust = .5, face="bold", size=15, colour = "grey60"),
        axis.text= element_text(size = 12),
        axis.title = element_text(size=15)) + 
  scale_fill_manual(values=c("darkgoldenrod4", "darkgoldenrod", "darkgoldenrod3", "darkgoldenrod2", "darkgoldenrod1", "goldenrod2", "goldenrod1"),
                    labels=c("LA","NC","SC","TX","VA","MS","AL"), name="Jackknifed")
house.hur
ggarrange(sen.hur, house.hur, common.legend = TRUE, legend = "right")
ggsave(file="./figures/figb7.pdf")